package io.ouka.rule.logic;

import io.ouka.rule.domain.TreeNodeLink;

import java.util.List;
import java.util.Map;

/**
 * Description:
 * User: Ouka
 * Date: 2020-06-08
 * Time: 21:32
 */
public abstract class BaseLogicFilter implements LogicFilter{
    @Override
    public Long doFilter(String value, List<TreeNodeLink> treeNodeLinks) {
        for (TreeNodeLink treeNodeLink : treeNodeLinks) {
            if (decisionLogic(value,treeNodeLink)) return treeNodeLink.getNodeTo();
        }
        return 0L;
    }

    @Override
    public  abstract String getValue(Long treeId, Map<String, String> keys);

    private boolean decisionLogic(String matterValue, TreeNodeLink nodeLink) {
        switch (nodeLink.getRuleType()) {
            case EQUAL:
                return matterValue.equals(nodeLink.getRuleValue());
            case MORE:
                return Double.parseDouble(matterValue) > Double.parseDouble(nodeLink.getRuleValue());
            case LESS:
                return Double.parseDouble(matterValue) < Double.parseDouble(nodeLink.getRuleValue());
            case LESSOREQUAL:
                return Double.parseDouble(matterValue) <= Double.parseDouble(nodeLink.getRuleValue());
            case MOREOREQUAL:
                return Double.parseDouble(matterValue) >= Double.parseDouble(nodeLink.getRuleValue());
            case SAME:
                return matterValue==nodeLink.getRuleValue();
                default:
                return false;
        }
    }
}
